package com.css.ids.server.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.css.ids.server.common.zip.ZipEntry;
import com.css.ids.server.common.zip.ZipOutputStream;

public class ZipAll {
	private int bufSize = Constants.bufSize;
	private String zipEncoding = Constants.zipEncoding;
	private ZipOutputStream zipStream = null;
	
	public static void main(String[] args) throws IOException {
		ZipAll zip = new ZipAll("D:\\zip.zip");
		zip.zip(new File("D:\\css_oa.log"))
			.zip(new File("D:\\test"))
			.zip("你好.xml", "你好大是大非测试dasdfadf111234")
			.close();
	}
	
	public ZipAll(){}
	public ZipAll(String zipFileName) throws FileNotFoundException{
        zipStream = init(zipFileName);
	}
	
	public ZipAll zip(File file) throws IOException {
		if (zipStream == null || file==null || !file.exists()) {
			return this;
		}
		compress(zipStream, file, ""); 
		return this;
	}
	public ZipAll zip(File[] files) throws IOException {
		if (zipStream == null || files==null) {
			return this;
		}
		// 压缩文件  
        for (File file : files) {
        	compress(zipStream, file, "");        	
        }
		return this;
	}
	public ZipAll zip(String fileName, String content) throws IOException {
		if (zipStream == null || fileName==null || content==null) {
			return this;
		}
		zipStream.putNextEntry(new ZipEntry(fileName));
		ByteArrayInputStream fis = new ByteArrayInputStream(content.getBytes("utf-8"));
		inStream2outStream(fis, zipStream);
		fis.close();
		return this;
	}
	public void close() throws IOException{
		if (zipStream != null) {
			zipStream.close();
		}
	}
	
	public ZipOutputStream init(String zipFileName) throws FileNotFoundException{
		// 未指定压缩文件名，默认为"ZipFile"  
        if (zipFileName == null || zipFileName.equals(""))  
            zipFileName = "ZipFile";  
  
        // 添加".zip"后缀  
        if (!zipFileName.endsWith(".zip"))  
            zipFileName += ".zip";

        // 创建文件夹  
        String dir = zipFileName.replace("/",File.separator).replace("\\",File.separator);
        int endIndex = dir.lastIndexOf(File.separator);  
        dir = dir.substring(0, endIndex);  
        File f = new File(dir);  
        f.mkdirs();
        //创建zip流
        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));  
        zos.setEncoding(zipEncoding);
        return zos;
	}
	
	private void compress(ZipOutputStream zos, File f, String fileName) throws IOException {  
		if(fileName==null || "".equals(fileName)){
			fileName = f.getName();
		}
        if (f.isDirectory()) {  
            // 压缩文件夹  
            File[] fl = f.listFiles();  
            zos.putNextEntry(new ZipEntry(fileName + "/"));  
            fileName = fileName.length() == 0 ? "" : fileName + "/";  
            for (int i = 0; i < fl.length; i++) {  
                compress(zos, fl[i], fileName + fl[i].getName());  
            }  
        } else {  
            // 压缩文件  
            zos.putNextEntry(new ZipEntry(fileName));  
            FileInputStream fis = new FileInputStream(f);  
            this.inStream2outStream(fis, zos);  
            fis.close();  
            zos.closeEntry();  
        }  
    }
	
	private void inStream2outStream(InputStream is, OutputStream os) throws IOException {  
        BufferedInputStream bis = new BufferedInputStream(is);  
        BufferedOutputStream bos = new BufferedOutputStream(os);  
        int bytesRead = 0;  
        for (byte[] buffer = new byte[bufSize]; ((bytesRead = bis.read(buffer)) != -1);) {  
            bos.write(buffer, 0, bytesRead); // 将流写入  
            bos.flush();
        }
        bos.flush();
    } 

	public String getZipEncoding() {
		return zipEncoding;
	}

	public void setZipEncoding(String zipEncoding) {
		if (zipStream != null) {
			zipStream.setEncoding(zipEncoding);
		}
		this.zipEncoding = zipEncoding;
	}
}
